<link href="http://<?= $_SERVER['HTTP_HOST'] ?>/salusme/include/css/fal_style.css" rel="stylesheet" type="text/css" />
<script src="http://<?= $_SERVER['HTTP_HOST'] ?>/salusme/include/js/jquery.js" type="text/javascript"></script>
<script src="http://<?= $_SERVER['HTTP_HOST'] ?>/salusme/include/js/flash.js" type="text/javascript"></script>
<script src="http://<?= $_SERVER['HTTP_HOST'] ?>/salusme/include/js/jquery-1.2.6.min.js" type="text/javascript"></script>
<?php
/*
 * This is success page from paypal payment.
 */
include '../config.php';
include '../db/pro_db.php';
$pro = new PRB();
$key = $pro->get_passkey();

session_start();
$user_name = $_SESSION['user_name'];
$sur_name = $_SESSION['sur_name'];
$password = $_SESSION['password'];
$email = $_SESSION['email'];
echo $buy_detail_id = $_SESSION['pro_detail_id'];
$pad = $_SESSION['amount'];
$item_id = $_GET['item_number'];
if ($item_id == '1') {
    $nxt = 'salusme';
}
if ($item_id == '2') {
    $nxt = 'salusbaby';
}

function _encode($password, $key) {
    $majorsalt = null;
    if ($key != '') {
        $_password = $key . $password;
    } else {
        $_password = $password;
    }
    if (function_exists('str_split')) {
        $_pass = str_split($_password);
    } else {
        $_pass = array();
        if (is_string($_password)) {
            for ($i = 0; $i < strlen($_password); $i++) {
                array_push($_pass, $_password[$i]);
            }
        }
    }

    foreach ($_pass as $_hashpass) {
        $majorsalt .= md5($_hashpass);
    }
    return $password = md5($majorsalt);
}

//$item_currency = $_GET['cc'];
?>
<title>Success</title>
<div id="header_small"></div>
<div id="wrapper">    
    <div id="mainContent">
Welcome, <b><?= $user_name ?></b><br/>
Payment successful...!!!<br/><br/><br/>

Now, please wait, while we are configuring your profile....
<?php
/*
 * inserting data to main user table.
 * just modify user_name = $username-->with $email.
 * 
 */
$password = _encode($password, $key);
mysql_query("insert into fa_user(`id`,`user_name`,`password`,`email`,`role`,`sur_name`) values(NULL,'" . $email . "', '" . $password . "','" . $email . "','user','" . $sur_name . "' )") or die(mysql_error());
//echo 'Done<br>';
$uid = mysql_insert_id();
//echo $uid;
/*
 * inserting data into buy_products.
 */
mysql_query("insert into salus_buy_product(`id`,`FK_fa_user_id`,`buy_detail_id`,`product_id`,`product_prize`,`date`) values(NULL,'" . $uid . "',$buy_detail_id,'".$item_id."','" . $pad . "','" . date('Y-m-d') . "') ") or die(mysql_error());
//echo "insert into salus_buy_product(`id`,`FK_fa_user_id`,`buy_detail_id`,`product_prize`,`date`) values(NULL,'" . $uid . "',$buy_detail_id,'" . $pad . "','" . date('Y-m-d') . "') ";
//echo 'Done<br>';
$path = $_SERVER['HTTP_HOST'] . '/' . $nxt;
if ($item_id == '2') {
    /*
     * inserting into child profile database
     */
    if ($_SESSION['gender'] == 'M') {
        $idd = 'father_id';
    } else {
        $idd = 'mother_id';
    }
    mysql_query("insert into baby_child_profile_detail (bid,buyer_id," . $idd . ") values(NULL,'" . $uid . "','" . $uid . "') ") or die(mysql_error());
    $bid = mysql_insert_id();
//    echo 'Done<br>';
    /*
     * creating folders on Baby.
     */
    $old = umask(0);
    $dir_path = '/home/salusban/www/salusbank.ch/salusbaby/storage/';
//    $dir_path = '/var/www/html/salusbaby/storage/';
    mkdir($dir_path . $bid, 0777) or die('Error1');
    mkdir($dir_path . $bid . '/folders', 0777) or die('Error2');
    mkdir($dir_path . $bid . '/folders/pregnancy', 0777) or die('Error3');
    mysql_query("insert into baby_folders(sr,bid,folder)values(NULL,$bid,'pregnancy')") or die(mysql_error());
    mkdir($dir_path . $bid . '/folders/pre-pregnancy', 0777) or die('Error4');    
    mysql_query("insert into baby_folders(sr,bid,folder)values(NULL,$bid,'pre-pregnancy')") or die(mysql_error());
    umask($old);
    
} elseif ($item_id == '1') {
    /*
     * creating folders on Me.
     */
    $old = umask(0);
    $dir_path = '/home/salusban/www/salusbank.ch/salusme/storage/';
//    $dir_path = '/var/www/html/salusme/storage/';
    mkdir($dir_path . $uid, 0777) or die('Error-me-1');
    mkdir($dir_path . $uid . '/folders', 0777) or die('Error-me-2');
    umask($old);
}
/*
 * inserting into user_profile
 */
mysql_query("insert into salus_user_profile_detail (id,FK_fa_user_id,name,sur_name,sex,email) value(NULL,'" . $uid . "','" . $user_name . "','" . $sur_name . "','" . $_SESSION['gender'] . "','" . $email . "') ") or die(mysql_error());
//echo 'Done<br>';

/*
 * pre-paid code
 */
//echo $prepaid_code = $_COOKIE['cde'];
if (isset($_COOKIE['cde'])) {
  $code_id = $_SESSION['code_id'];
    mysql_query("update salus_prepaid_codes set user_id='".$uid."' where prepaid_code ='".$prepaid_code."' ") or die(mysql_error());
//    echo 'Done<br>';
}
setcookie("cde",'',time()-3600);
session_destroy();
session_unset();
echo 'Account created successfully. goto <a href=http://' . $path . '> <b>' . $nxt . '</b></a> for login to your profile.';
?>
    </div>
</div>